<?php 
require_once 'DataBase/config.php';
class DataBase {
    public $host;
    private $username;
    private $password;
    private $database;
    
    private $query_id = 0;
    private $link_id = 0;
    private $affected_rows = 0;
    var $error = "";
    var $errno = 0;
    
    function get_link() {
        return $this->link_id;
    }
    function get_affected_rows() {
        return $this->affected_rows;
    }
    function Database() {
        $this->host = DB_HOST;
        $this->username = DB_USER;
        $this->password = DB_PASS;
        $this->database = DB_NAME;
    }
    /*
     function Database($host, $username, $password, $database) {
     $this->host = $host;
     $this->username = $username;
     $this->password = $password;
     $this->database = $database;
     }
     */
    function connect() {
        $this->link_id = @mysqli_connect($this->host, $this->username, $this->password, $this->database);
        
        if (!$this->link_id) {//open failed
            $this->oops("Could not connect to server: <b>$this->host</b>.");
        }
        // unset the data so it can't be dumped
        $this->server = '';
        $this->user = '';
        $this->pass = '';
        $this->database = '';
        
    }
    
    function query($sql) {
        // do query
        $this->query_id = @mysqli_query($this->link_id, $sql);
        
        if (!$this->query_id) {//query failed
            $this->oops("<b>MySQL Query fail:</b> $sql");
            return 0;
        }
        $this->affected_rows = @mysqli_affected_rows($this->link_id);
        return $this->query_id;
    }
    
    function close() {
        if (!@mysqli_close($this->link_id)) {
            $this->oops("Connection close failed.");
        }
    }

    
    function oops($msg = '') { //parse error
        if ($this->link_id > 0) {
            $this->error = mysqli_error($this->link_id);
            $this->errno = mysqli_errno($this->link_id);
        } else {
            $this->error = mysqli_error();
            $this->errno = mysqli_errno();
        }
        
?>
<table align="center" border="1" cellspacing="0" style="background:white;color:black;width:80%;">
    <tr>
        <th colspan=2>
            Database Error
        </th>
    </tr>
    <tr>
        <td align="right" valign="top">
            Message:
        </td>
        <td>
            <?php echo $msg; ?>
        </td>
    </tr>
    <?php if (strlen($this->error) > 0) echo '<tr><td align="right" valign="top" nowrap>MySQL Error:</td><td>'.$this->error.'</td></tr>'; ?>
    <tr>
        <td align="right">
            Date:
        </td>
        <td>
            <?php echo date("l, F j, Y \a\\t g:i:s A"); ?>
        </td>
    </tr>
    <tr>
        <td align="right">
            Script:
        </td>
        <td>
            <a href="<?php echo @$_SERVER['REQUEST_URI']; ?>"><?php echo @$_SERVER['REQUEST_URI']; ?></a>
        </td>
    </tr>
    <?php if (strlen(@$_SERVER['HTTP_REFERER']) > 0) echo '<tr><td align="right">Referer:</td><td><a href="'.@$_SERVER['HTTP_REFERER'].'">'.@$_SERVER['HTTP_REFERER'].'</a></td></tr>'; ?>
</table>
<?php 
}#-#oops()

}
?>
